local hx__xianbian = fk.CreateSkill {
  name = "hx__xianbian",
}


hx__xianbian:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__xianbian.name)
  end,
  on_cost = function(self, event, target, player, data)
    local choices = {"hx__basic", "hx__equip", "hx__trick"}
    local choice = player.room:askToChoice(player,{
      choices = choices,
      skill_name = hx__xianbian.name,
    })
    if choice then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player,"@hx__xianbian", 1)
    if event:getCostData(self) == "hx__basic" then
      player.room:addPlayerMark(player, "@@hx__xianbian_b", 1)
    elseif event:getCostData(self) == "hx__trick" then
      player.room:addPlayerMark(player,"@@hx__xianbian_t",1)
    elseif event:getCostData(self) == "hx__equip" then
      player.room:addPlayerMark(player,"@@hx__xianbian_e",1)
    end
  end,
})

hx__xianbian:addEffect(fk.CardUsing, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    local id_b = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeBasic end)
    local id_t = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeTrick end)
    local id_e = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeEquip end)
    if player:hasSkill(hx__xianbian.name) and data.card and player:usedEffectTimes(self.name, Player.HistoryTurn) < 2 then
      if player:getMark("@@hx__xianbian_b") > 0 and #id_b > 0 then
        return data.card.type == Card.TypeTrick or data.card.type == Card.TypeEquip
      elseif player:getMark("@@hx__xianbian_t") > 0 and #id_t > 0 then
        return data.card.type == Card.TypeEquip or data.card.type == Card.TypeBasic
      elseif player:getMark("@@hx__xianbian_e") > 0 and #id_e > 0 then
        return data.card.type == Card.TypeBasic or data.card.type == Card.TypeTrick
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#hx__xianbian-choosetarget:::"..data.card:toLogString()
    if player:getMark("@@hx__xianbian_b") > 0 then
      local card = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        skill_name = hx__xianbian.name,
        pattern = ".|.|.|.|.|basic",
        cancelable = false,
        include_equip = true,
      })
      if #card > 0 then
        room:addPlayerMark(player,"@hx__xianbian", 1)
        player:drawCards(1, hx__xianbian.name)
      end
    elseif player:getMark("@@hx__xianbian_t") > 0 then
      local card = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        skill_name = hx__xianbian.name,
        pattern = ".|.|.|.|.|trick",
        cancelable = false,
        include_equip = true,
      })
      if #card > 0 then
        room:addPlayerMark(player,"@hx__xianbian",1)
        player:drawCards(1, hx__xianbian.name)
      end
    elseif player:getMark("@@hx__xianbian_e") > 0 then
      local card = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        skill_name = hx__xianbian.name,
        pattern = ".|.|.|.|.|equip",
        cancelable = false,
        include_equip = true,
      })
      if #card > 0 then
        room:addPlayerMark(player,"@hx__xianbian", 1)
        player:drawCards(1, hx__xianbian.name)
      end
    end
    local target_p = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = 1,
      skill_name = hx__xianbian.name,
      prompt = prompt,
    })
    if #target_p > 0 then
      event:setCostData(self, {
        newTarget = target_p[1],
        cardName = data.card.trueName
      })
      data.tos = { event:getCostData(self).newTarget }
    end
    room:loseHp(player,1,hx__xianbian.name)
  end,
})


return hx__xianbian